# 电影院用户表
# 这里权限换一种写法（用户表、权限表、用户权限表）
from werkzeug.security import generate_password_hash, check_password_hash
from App.ext import db
from App.models import BaseModel

# 用户表
class CinemaUser(BaseModel):
    username = db.Column(db.String(32) , unique=True)
    _password = db.Column(db.String(256))
    phone = db.Column(db.String(32) , unique=True)
    is_delete = db.Column(db.Boolean , default=False)
    is_verify = db.Column(db.Boolean , default = False)     # 权限。默认都是没有，只有通过超管给才能进行后续一些高级操作

    @property
    def password(self):
        raise Exception('不给你访问')
    @password.setter
    def password(self , password_value_parameter):
        self._password = generate_password_hash(password_value_parameter)

    # 密码哈希检测
    def check_password(self , password_value_parameter):
        return check_password_hash(self._password , password_value_parameter)
    def check_permission(self , permission):
        if not self.is_verify:
            return False
        permissions = CinemaUserPermission.query.filter(CinemaUserPermission.c_user_id == self.id)
        for i in permissions:
            # if Permissions.query.get(i.c_permission_id) == permission:
            if i.c_permission_id == permission:
                return True
        return False


# 权限表
class Permissions(BaseModel):
    p_name = db.Column(db.String(64) , unique=True)

# 用户权限表（用户和权限是多对多的关系）
class CinemaUserPermission(BaseModel):
    c_user_id = db.Column(db.Integer , db.ForeignKey(CinemaUser.id))
    c_permission_id = db.Column(db.Integer , db.ForeignKey(Permissions.id))


